from utils.excel_util import ExcelUtil
from model.sysRoleModel import SysRole
from schema.sysRoleSchema import SysRoleCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysRoleExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "role_name", "title": "角色名称", "required": True},

        {"field": "role_key", "title": "角色权限字符串", "required": True},

        {"field": "role_sort", "title": "显示顺序", "required": True},

        {"field": "data_scope", "title": "数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）", "required": False},

        {"field": "menu_check_strictly", "title": "菜单树选择项是否关联显示", "required": False},

        {"field": "dept_check_strictly", "title": "部门树选择项是否关联显示", "required": False},

        {"field": "status", "title": "状态（0正常 2删除）", "required": True},

        {"field": "use_flag", "title": "角色状态（0正常 1停用）", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

        {"field": "remark", "title": "备注", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "role_name", "title": "角色名称"},

            {"field": "role_key", "title": "角色权限字符串"},

            {"field": "role_sort", "title": "显示顺序"},

            {"field": "data_scope", "title": "数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）"},

            {"field": "menu_check_strictly", "title": "菜单树选择项是否关联显示"},

            {"field": "dept_check_strictly", "title": "部门树选择项是否关联显示"},

            {"field": "status", "title": "状态（0正常 2删除）"},

            {"field": "use_flag", "title": "角色状态（0正常 1停用）"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

            {"field": "remark", "title": "备注"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "role_name": "example",

            "role_key": "example",

            "role_sort": 1,

            "data_scope": "A",

            "menu_check_strictly": 1,

            "dept_check_strictly": 1,

            "status": "A",

            "use_flag": "A",

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

            "remark": "example",

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="角色信息表导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="角色信息表数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysRoleCreate
        )